#!/opt/anaconda3/bin/python
# -*- coding:utf-8 -*-
'''
@project : meiguo_mall
@author  : leftxxk
#@file   : its.py
#@time   : 2021-06-04 13:18:21
'''
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer

from meiguo_mall import settings


def generic_openid(openid):
    s = Serializer(secret_key=settings.SECRET_KEY, expires_in=3600)
    access_token = s.dumps({'openid': openid})
    return access_token.decode()  # 将bytes类型数据转换为str


def check_access(access_token):
    s = Serializer(secret_key=settings.SECRET_KEY, expires_in=3600)
    try:
        access_token = s.loads(access_token)
    except Exception:
        return None
    else:
        return access_token.get('openid')


def generic_email_verify_token(user_id):
    s = Serializer(secret_key=settings.SECRET_KEY, expires_in=3600)
    access_token = s.dumps({'user_id': user_id})
    return access_token.decode()  # 将bytes类型数据转换为str



def check_email_verify_token(token):
    s = Serializer(secret_key=settings.SECRET_KEY, expires_in=3600)
    try:
        res = s.loads(token)
    except Exception:
        return None
    else:
        return res.get('user_id')